#include <allegro.h>
#include "general.h"
#include "collision.h"


int main() 
{
SAMPLE *sound;
SAMPLE *effet;
BITMAP *Buffer;
BITMAP *Background;
Personnage p;
coins c,c1,c2,c3,c4,c5,c6;
obstacle o1;
obstacle o2;
int i,j,compteur;


allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
Buffer = create_bitmap(800, 600);
 /*
install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
set_volume(255, 255);//Volume
 sound = load_sample("./son/chuck.wav");//chargement de la musique
 effet = load_sample("./son/effet.wav");
play_sample(sound, 255, 128, 1000, 0);*/

//chargement du background
Background = load_bitmap("./images/background.bmp", NULL);

//chargement des images
chargement_image_personnage(&p);
chargement_coins(&c1);
chargement_coins(&c2);
chargement_coins(&c3);
chargement_coins(&c4);
chargement_coins(&c5);
chargement_coins(&c6);
chargement_obstacle(&o1);
chargement_obstacle(&o2);

//initialisation des variables
o1.x = 400;
o1.y = 300;
o1.n=1;

o2.x = 500;
o2.y = 400;
o2.n=1;

c1.x = 300;
c1.y = 200;
c1.n=1;

c2.x = 300;
c2.y = 300;
c2.n=1;

c3.x = 400;
c3.y = 200;
c3.n=1;

c4.x = 500;
c4.y = 300;
c4.n=1;

c5.x = 600;
c5.y = 400;
c5.n=1;

c6.x = 700;
c6.y = 50;
c6.n=1;

p.x = 0;
p.y = 200;
p.direction=0;
i=0;
compteur=0;
p.score=0;


while (!key[KEY_ESC]) {
//personnage
int sprite1_x=p.x;
int sprite1_y=p.y;
int sprite1_w=p.img_right[0]->w;
int sprite1_h=p.img_right[0]->h;

//obstacle1
int spriteo1_x=o1.x;
int spriteo1_y=o1.y;
int spriteo1_w=o1.img_obstacle->w;   
int spriteo1_h=o1.img_obstacle->h;

//obstacle2

int spriteo2_x=o2.x;
int spriteo2_y=o2.y;
int spriteo2_w=o2.img_obstacle->w;   
int spriteo2_h=o2.img_obstacle->h;

//coins1
int sprite2_x=c1.x;
int sprite2_y=c1.y;
int sprite2_w=c1.img_coins->w;   
int sprite2_h=c1.img_coins->h;
 
//coins2
int sprite3_x=c2.x;
int sprite3_y=c2.y;
int sprite3_w=c2.img_coins->w;   
int sprite3_h=c2.img_coins->h;

//coins3
int sprite4_x=c3.x;
int sprite4_y=c3.y;
int sprite4_w=c3.img_coins->w;   
int sprite4_h=c3.img_coins->h;

//coins4
int sprite5_x=c4.x;
int sprite5_y=c4.y;
int sprite5_w=c4.img_coins->w;   
int sprite5_h=c4.img_coins->h;

//coins5
int sprite6_x=c5.x;
int sprite6_y=c5.y;
int sprite6_w=c5.img_coins->w;   
int sprite6_h=c5.img_coins->h;

//coins6
int sprite7_x=c6.x;
int sprite7_y=c6.y;
int sprite7_w=c6.img_coins->w;   
int sprite7_h=c6.img_coins->h;


//dessiner le background
blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);

//tester s'il ya collision
//Coins :
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite2_x,  sprite2_y,  sprite2_w,  sprite2_h) !=0 )   c1.n=0;  
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite3_x,  sprite3_y,  sprite3_w,  sprite3_h) !=0 )   c2.n=0;   
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite4_x,  sprite4_y,  sprite4_w,  sprite4_h) !=0 )   c3.n=0;
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite5_x,  sprite5_y,  sprite5_w,  sprite5_h) !=0 )   c4.n=0;
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite6_x,  sprite6_y,  sprite6_w,  sprite6_h) !=0 )   c5.n=0;
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite7_x,  sprite7_y,  sprite7_w,  sprite7_h) !=0 )   c6.n=0;

//Obstacle :
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  spriteo1_x,  spriteo1_y,  spriteo1_w,  spriteo1_h) !=0 )    o1.n=0;  

if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  spriteo2_x,  spriteo2_y,  spriteo2_w,  spriteo2_h) !=0 )    o2.n=0;  

//dessiner les coins s'il n'ya pas collision
if(c1.n==1){
draw_sprite(Buffer, c1.img_coins, c1.x ,c1.y);
}

if(c2.n==1){
draw_sprite(Buffer, c2.img_coins, c2.x ,c2.y);
}

if(c3.n==1){
draw_sprite(Buffer, c3.img_coins, c3.x ,c3.y);
}

if(c4.n==1){
draw_sprite(Buffer, c4.img_coins, c4.x ,c4.y);
}

if(c5.n==1){
draw_sprite(Buffer, c5.img_coins, c5.x ,c5.y);
}

if(c6.n==1){
draw_sprite(Buffer, c6.img_coins, c6.x ,c6.y);
}
///////////////////////////////////////

//dessiner l'obstacle s'il n'ya pas collision
if(o1.n==1){
draw_sprite(Buffer, o1.img_obstacle, o1.x ,o1.y);
}else //Loosing
{
allegro_message("Game Over !! %s", allegro_error) ;
return 1;
}
//obstacle 2
if(o2.n==1){
draw_sprite(Buffer, o2.img_obstacle, o2.x ,o2.y);
}else //Loosing
{
allegro_message("Game Over !! %s", allegro_error) ;
return 1;
}

/*//bouger l'obstacle
do{
o1.x--;
if (o1.x == 1 ) 
{
o1.x = tx[0];
o1.y = ty[compteur];
compteur++;
}
if (compteur == 6){compteur=0;}
}while (o1.x < 0 && compteur == -1);*/

//Mouvement

if (key[KEY_RIGHT] && p.x < SCREEN_W - p.img_right[0]->w) {
i++;
p.x++;

p.direction=2;
}

/*if (key[KEY_LEFT] && p.x >0) {
i--;
p.x--;

p.direction=1;
}*/

if (key[KEY_UP] && p.y > 0) {
p.y--;
p.direction=0;
}

if (key[KEY_DOWN] && p.y <SCREEN_H - p.img_right[0]->h) {
p.y++;
p.direction=0;
}

//dessiner le personnage suivant la direction

if(p.direction==0){
draw_sprite(Buffer, p.img_right[i % 3], p.x, p.y);
}else if(p.direction==2){
draw_sprite(Buffer, p.img_right[i%3], p.x, p.y);
}
else{
draw_sprite(Buffer, p.img_left[i % 3], p.x, p.y);
}

////////////////////////////////////////

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
clear_bitmap(Buffer);
		

if(i==1000){
i=0;
}


}//fin while


for(j=0;j<3;j++){
destroy_bitmap(p.img_right[j]);
destroy_bitmap(p.img_left[j]);
destroy_bitmap(p.img_fix[j]);
}
destroy_bitmap(Buffer);
destroy_bitmap(c1.img_coins);
destroy_bitmap(c2.img_coins);
destroy_bitmap(c3.img_coins);
destroy_bitmap(c4.img_coins);
destroy_bitmap(c5.img_coins);
destroy_bitmap(c6.img_coins);
destroy_bitmap(o1.img_obstacle);
destroy_bitmap(o2.img_obstacle);


return 0;

}//fin main






